package com.coocaa.app.lib.cleaner.mgr;

import com.coocaa.app.lib.cleaner.mgr.impl.DataHolderImpl;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lu on 17-4-12.
 */

public class DataHolderLoaderTest {
    @Before
    public void setUp() throws Exception {


    }

    @Test
    public void appendAPKandLoadTest() throws Exception {
        DataHolder.DataHolderListener listener = Mockito.mock(DataHolder.DataHolderListener.class);
        DataHolder holder = DataHolderImpl.instance;
        DataLoader loader = DataHolderImpl.instance;
        holder.reset(listener);

        List<String> apks = new ArrayList<String>();
        for (int i = 0; i < 10; i++)
            apks.add("apkFile:" + i);
        for (String apk : apks)
            holder.appendAPK(apk);

        Thread.sleep(1000);
        for (String apk : apks)
            Mockito.verify(listener, Mockito.times(1)).onApkRubbishUpdate(Mockito.any(DataHolder.ApkRubbish.class), Mockito.eq(apk));

        DataHolder.ApkRubbish rubbish = loader.getApkRubbish();
        Assert.assertNotNull(rubbish);
        Assert.assertTrue(rubbish.apks.equals(apks));
        apks.add("zxczxczxc");
        Assert.assertFalse(rubbish.apks.equals(apks));
    }

    @Test
    public void appendPackageCacheandLoadTest() throws Exception {
        DataHolder.DataHolderListener listener = Mockito.mock(DataHolder.DataHolderListener.class);
        DataHolder holder = DataHolderImpl.instance;
        DataLoader loader = DataHolderImpl.instance;
        holder.reset(listener);

        class A {
            public String packageName;
            public long size;

            public A(String packageName, long size) {
                this.packageName = packageName;
                this.size = size;
            }
        }
        List<A> aa = new ArrayList<A>();
        for (int i = 0; i < 10; i++)
            aa.add(new A("p" + i, (long) (Math.random() * 1000) + 100));
        for (A a : aa)
            holder.appendPackageCache(a.packageName, a.size);
        Thread.sleep(1000);
        for (A a : aa)
            Mockito.verify(listener, Mockito.times(1)).onPackageCacheUpdate(Mockito.any(DataHolder.PackageCache.class), Mockito.eq(a.packageName));

        DataHolder.PackageCache cache = loader.getPackageCache();
        Assert.assertNotNull(cache);
        long _size = 0;
        for (A a : aa)
            _size += a.size;
        Assert.assertEquals(_size, cache.size);
    }

    @Test
    public void appendPackageDataandLoadTest() throws Exception {
        DataHolder.DataHolderListener listener = Mockito.mock(DataHolder.DataHolderListener.class);
        DataHolder holder = DataHolderImpl.instance;
        DataLoader loader = DataHolderImpl.instance;
        holder.reset(listener);

        class A {
            public String packageName;
            public long size;

            public A(String packageName) {
                this.packageName = packageName;
                this.size = (long) (Math.random() * 1000 + 100);
            }
        }
        List<A> aa = new ArrayList<A>();
        for (int i = 0; i < 10; i++) {
            aa.add(new A("p" + i));
        }
        for (A a : aa)
            holder.appendPackageData(a.packageName, a.size);
        Thread.sleep(1000);
        for (A a : aa)
            Mockito.verify(listener, Mockito.times(1)).onPackageDataUpdate(Mockito.any(DataHolder.PackageData.class), Mockito.eq(a.packageName));

        DataHolder.PackageData data = loader.getPackageData();
        Assert.assertNotNull(data);

        long _size = 0;
        for (A a : aa) {
            _size += a.size;
        }
        Assert.assertEquals(_size, data.size);
    }

    @Test
    public void appendOtherDataandLoadTest() throws Exception {
        DataHolder.DataHolderListener listener = Mockito.mock(DataHolder.DataHolderListener.class);
        DataHolder holder = DataHolderImpl.instance;
        DataLoader loader = DataHolderImpl.instance;
        holder.reset(listener);

        List<String> aa = new ArrayList<String>();
        for (int i = 0; i < 10; i++)
            aa.add("file:" + i);

        for (String a : aa)
            holder.appendOtherData(a);

        Thread.sleep(1000);
        for (String a : aa)
            Mockito.verify(listener, Mockito.times(1)).onOtherDataUpdate(Mockito.any(DataHolder.OtherData.class), Mockito.eq(a));

        DataHolder.OtherData other = loader.getOtherData();
        Assert.assertNotNull(other);
        Assert.assertTrue(aa.equals(other.datas));
        aa.add("123213123");
        Assert.assertFalse(aa.equals(other.datas));

    }
}
